package com.cgtong.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.SslErrorHandler;
import android.net.http.SslError;
import android.os.Build;

import com.cgtong.android.R;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.http.ApiManager;
import com.cgtong.android.util.PostUtil;
import com.kingja.loadsir.callback.SuccessCallback;

import butterknife.BindView;

/**
 * 首页Banner 详情页
 * Created by Sky.Bing on 2017/10/24.
 */

public class WebViewActivity extends BaseTitleActivity {
    public static final String EXTRA_LOAD_URL = "load_url";
    public static final String EXTRA_FROM ="from";

    @BindView(R.id.wv_news_info)
    public WebView mNewsInfoWebView;

    @Override
    protected String getContentTitle() {
        return getResources().getString(R.string.txt_title_banner_info);
    }

    public static void actionStart(Activity context, String loadUrl) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra(EXTRA_LOAD_URL, loadUrl);
        context.startActivity(intent);
    }

    public static void actionStart(Activity context, String loadUrl,String from) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra(EXTRA_LOAD_URL, loadUrl);
        intent.putExtra(EXTRA_FROM,from);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_news_info;
    }

    private String mFromIndex;
    @Override
    protected void loadDataByNet() {
        Intent intent = getIntent();

        WebSettings wSet = mNewsInfoWebView.getSettings();
        wSet.setJavaScriptEnabled(true);
        wSet.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        wSet.setDefaultTextEncodingName("UTF-8");
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
                wSet.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }
            mNewsInfoWebView.removeJavascriptInterface("searchBoxJavaBridge_");
            mNewsInfoWebView.removeJavascriptInterface("accessibility");
            mNewsInfoWebView.removeJavascriptInterface("accessibilityTraversal");
        } catch (Throwable tr) {
            tr.printStackTrace();
        }
        String loadUrl = intent.getStringExtra(EXTRA_LOAD_URL);
        if(loadUrl.equals(ApiManager.PROBLEM_SBFW_URL)){
            mTitleCenterTxt.setText(getResources().getString(R.string.txt_question_classify));
        }else if(loadUrl.equals(ApiManager.PROBLEM_YDSM_URL)){
            mTitleCenterTxt.setText(getResources().getString(R.string.txt_lead_explain));
        }
        mFromIndex = intent.getStringExtra(EXTRA_FROM);
        mNewsInfoWebView.loadUrl(loadUrl);
        mNewsInfoWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
            }
            @Override
            public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
        mNewsInfoWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mNewsInfoWebView.canGoBack()) {
            mNewsInfoWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDataReloadByNet(View v) {
    }

    @Override
    protected void backClick() {
        if("ad".equals(mFromIndex)){
            Intent intent = new Intent(WebViewActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }else{
            super.backClick();
        }
    }

    @Override
    public void onBackPressed() {
        backClick();
    }
}